﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Database
{
    public partial class Orders : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            CoffeeShopEntities1 db = new CoffeeShopEntities1();
            if (Request.QueryString["ID"] != null)
            {
                Guid id = Guid.Parse(Request.QueryString["ID"]);
                Order order = (from o in db.Orders.Include("OrderItems").Include("Customer")
                               where o.ID == id
                               select o).Single();
                form1.Controls.Add(new LiteralControl(String.Format("<h1>Order for: {0}</h1>", order.Customer.Name)));
                form1.Controls.Add(
                    new LiteralControl("<table><tr><td>#</td><td>Item</td><td>Price</td><td>Total</td></tr>"));
                decimal total = 0;
                foreach (OrderItem oi in order.OrderItems)
                {
                    Coffee coffee = (from cof in db.Coffees
                                     where cof.ID == oi.ItemID
                                     select cof).SingleOrDefault();
                    Cake cake = (from cak in db.Cakes
                                 where cak.ID == oi.ItemID
                                 select cak).SingleOrDefault();
                    if (coffee != null)
                    {
                        form1.Controls.Add(
                            new LiteralControl(
                                String.Format("<tr><td>{2}</td><td>{0}</td><td>{1:C}</td><td>{3:C}</td></tr>", 
                                coffee.Name, coffee.Price, oi.Count, coffee.Price*oi.Count)));
                        total += coffee.Price * oi.Count;
                    }
                    if (cake != null)
                    {
                        form1.Controls.Add(
                            new LiteralControl(
                                String.Format("<tr><td>{2}</td><td>{0}</td><td>{1:C}</td><td>{3:C}</td></tr>",
                                cake.Name, cake.Price, oi.Count, cake.Price * oi.Count)));
                        total += cake.Price * oi.Count;
                    }
                }
                form1.Controls.Add(
                    new LiteralControl(
                        String.Format("<tr><td></td><td></td><td></td><td>{0:C}</td></tr></table>", total)));
            }
            else
            {
                foreach (var order in from o in db.Orders
                                      join c in db.Customers
                                      on o.CustomerID equals c.ID
                                      select new
                                      {
                                          CustomerName = c.Name,
                                          OrderId = o.ID
                                      })
                {
                    HyperLink link = new HyperLink();
                    link.Text = order.CustomerName;
                    link.NavigateUrl = String.Format("~/Orders.aspx?ID={0}", Server.UrlEncode(order.OrderId.ToString()));
                    form1.Controls.Add(link);
                    form1.Controls.Add(new LiteralControl("<br />"));
                }
            }
        }
    }
}